package com.hawk.eye.client.logback.encoder;

import ch.qos.logback.classic.spi.ILoggingEvent;
import com.hawk.eye.client.logback.provider.*;
import net.logstash.logback.composite.JsonProviders;
import net.logstash.logback.composite.loggingevent.*;
import net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder;
import net.logstash.logback.stacktrace.ShortenedThrowableConverter;

import java.util.TimeZone;

/**
 * @className: HawkEyeLoggingEventCompositeJsonEncoder
 * @description: 内置了一部分日志信息的Encoder
 * @author: salad
 * @date: 2023/10/8
 **/
public class HawkEyeLoggingEventCompositeJsonEncoder extends LoggingEventCompositeJsonEncoder {

    @Override
    public void start() {
        JsonProviders<ILoggingEvent> providers = this.getProviders();
        providers.addProvider(new ErrorFileProvider());
        providers.addProvider(new ErrorNameProvider());
        LoggingEventThreadNameJsonProvider threadNameJsonProvider = new LoggingEventThreadNameJsonProvider();
        threadNameJsonProvider.setFieldName("threadName");
        providers.addProvider(threadNameJsonProvider);
        LogLevelJsonProvider logLevelJsonProvider = new LogLevelJsonProvider();
        logLevelJsonProvider.setFieldName("logLevel");
        providers.addProvider(logLevelJsonProvider);
        MessageJsonProvider messageJsonProvider = new MessageJsonProvider();
        messageJsonProvider.setFieldName("errorMessage");
        providers.addProvider(messageJsonProvider);
        LoggerNameJsonProvider loggerNameJsonProvider = new LoggerNameJsonProvider();
        loggerNameJsonProvider.setFieldName("className");
        providers.addProvider(loggerNameJsonProvider);
        LoggingEventFormattedTimestampJsonProvider timestampJsonProvider = new LoggingEventFormattedTimestampJsonProvider();
        timestampJsonProvider.setPattern("yyyy-MM-dd HH:mm:ss.SSS");
        providers.addProvider(timestampJsonProvider);
        providers.addProvider(new PIDProvider());
        providers.addProvider(new MdcJsonProvider());
        providers.addProvider(new LoggingEventPatternJsonProvider());
        providers.addProvider(new ArgumentsJsonProvider());
        // 添加 stackHash provider
        StackHashJsonProvider stackHashProvider = new StackHashJsonProvider();
        stackHashProvider.setFieldName("stackHash");
        providers.addProvider(stackHashProvider);
        // 添加 stackTrace provider
        StackTraceJsonProvider stackTraceProvider = new StackTraceJsonProvider();
        stackTraceProvider.setFieldName("stackTrace");
        // 配置 ShortenedThrowableConverter
        ShortenedThrowableConverter stc = new ShortenedThrowableConverter();
        stc.setMaxDepthPerThrowable(50);
        stc.setMaxLength(2048);
        stc.setShortenedClassNameLength(20);
        stc.addExclude("net.sf.cglib.proxy.MethodProxy.invoke");
        stc.addExclude("org.apache.*");
        stc.addExclude("sun.reflect.*");
        stc.addExclude("java.lang.reflect.*");
        stc.setRootCauseFirst(true);
        stackTraceProvider.setThrowableConverter(stc);
        providers.addProvider(stackTraceProvider);
        super.start();
    }

}
